package com.lzy.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzy.base.BaseInfoProperties;
import com.lzy.mapper.FriendCircleLikedMapper;
import com.lzy.mapper.FriendCircleMapper;
import com.lzy.mapper.custom.FriendCircleMapperCustom;
import com.lzy.pojo.Comment;
import com.lzy.pojo.FriendCircle;
import com.lzy.pojo.FriendCircleLiked;
import com.lzy.pojo.Users;
import com.lzy.pojo.bo.FriendCircleBO;
import com.lzy.pojo.vo.FriendCircleVO;
import com.lzy.service.FriendCircleService;
import com.lzy.service.UsersService;
import com.lzy.utils.PagedGridResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author lzy
 * @since 2025-08-26
 */
@Service
public class FriendCircleServiceImpl extends BaseInfoProperties implements FriendCircleService {

    @Autowired
    private FriendCircleMapper friendCircleMapper;

    @Autowired
    private FriendCircleLikedMapper likedMapper;

    @Autowired
    private FriendCircleMapperCustom friendCircleMapperCustom;

    @Autowired
    private UsersService usersService;
    @Transactional
    @Override
    public void publish(FriendCircleBO friendCircleBO) {

        FriendCircle pendingFriendCircle = new FriendCircle();

        BeanUtils.copyProperties(friendCircleBO,pendingFriendCircle);

        friendCircleMapper.insert(pendingFriendCircle);
    }

    @Override
    public PagedGridResult queryList(String userId, Integer page, Integer pageSize) {

        Map<String,Object> map = new HashMap<>();

        map.put("mySelfId",userId);
        Page<FriendCircleVO> pageInfo = new Page<>(page,pageSize);

        friendCircleMapperCustom.queryFriendCircleList(pageInfo,map);
        return setterPagedGridPlus(pageInfo);
    }

    @Transactional
    @Override
    public void like(String friendCircleId, String userId) {

        // 根据朋友圈的主键ID获得发布人的id
        FriendCircle friendCircle = queryById(friendCircleId);
        // 根据发布人的id获得发布人的网名
        Users user = usersService.getById(friendCircle.getUserId());


        FriendCircleLiked circleLiked = new FriendCircleLiked();
        circleLiked.setFriendCircleId(friendCircleId);
        circleLiked.setLikedUserId(userId); //点赞人的id
        circleLiked.setBelongUserId(friendCircle.getUserId());
        circleLiked.setLikedUserName(user.getNickname());
        circleLiked.setCreatedTime(LocalDateTime.now());
        likedMapper.insert(circleLiked);

        //点赞过后，朋友圈对应的点赞数加1
        redis.increment(REDIS_FRIEND_CIRCLE_LIKED_COUNTS + ":" + friendCircleId,1);
        //标记是哪些用户点赞
        redis.setnx(REDIS_DOES_USER_LIKE_FRIEND_CIRCLE + ":" + friendCircleId + ":" + userId,userId);
    }

    @Transactional
    @Override
    public void unlike(String friendCircleId, String userId) {

        //从数据库删除点赞关系
        QueryWrapper<FriendCircleLiked> queryWrapper = new QueryWrapper<FriendCircleLiked>()
                .eq("friend_circle_id",friendCircleId)
                .eq("liked_user_id",userId);
        likedMapper.delete(queryWrapper);

        //取消点赞后，朋友圈对应的点赞数减1
        redis.decrement(REDIS_FRIEND_CIRCLE_LIKED_COUNTS + ":" + friendCircleId,1);

        //删除标记的点赞
        redis.del(REDIS_DOES_USER_LIKE_FRIEND_CIRCLE + ":" + friendCircleId + ":" + userId);
    }

    @Override
    public List<FriendCircleLiked> queryLikedFriends(String friendCircleId) {

        QueryWrapper<FriendCircleLiked> queryWrapper = new QueryWrapper<FriendCircleLiked>()
                .eq("friend_circle_id",friendCircleId);

        return likedMapper.selectList(queryWrapper);
    }

    @Override
    public Boolean doILike(String friendCircleId, String userId) {
        String isExist = redis.get(REDIS_DOES_USER_LIKE_FRIEND_CIRCLE + ":" + friendCircleId + ":" + userId);
        return StringUtils.isNotBlank(isExist);
    }

    @Transactional
    @Override
    public void delete(String friendCircleId, String userId) {
        QueryWrapper<FriendCircle> deleteWrapper = new QueryWrapper<FriendCircle>()
                .eq("id",friendCircleId)
                .eq("user_id",userId);
        friendCircleMapper.delete(deleteWrapper);
    }

    private FriendCircle queryById(String friendCircleId){
        return friendCircleMapper.selectById(friendCircleId);
    }
}
